#include "Greedy2.h"

void Greedy2::nextStep() {
    int nid = nodeToExplore();
    TVec<TInt> rec;
    if (visit[nid].Val1 > visit[nid].Val2) {
      visit[nid].Val1 = 0;
      network->getAllFollowers(nid, rec);
    } else {
      visit[nid].Val2 = 0;
      network->getAllFollowees(nid, rec);
    }
    for(TVec<TInt>::TIter i = rec.BegI(); i != rec.EndI(); i++)
        updateDiscoveredNode(i->Val);
    TIntPr p = network->evaluate();
    int totalCost = network->getTotalCost();
    //if(totalCost % 1000 == 0){
    nodeRecall.Add(TFltPr(network->getTotalCost(), double(p.Val1)));
    edgeRecall.Add(TFltPr(network->getTotalCost(), double(p.Val2)));
    //}
}

int Greedy2::nodeToExplore() {
  int nid;
  if (!q.empty()) {
    nid = q.top().Val2;
    q.pop();
  } else {
    nid = network->getTeleportNode();
    updateDiscoveredNode(nid);
  }
  return nid;
}

void Greedy2::updateDiscoveredNode(int discoveredNode) {
  if (visit.find(discoveredNode) == visit.end()) {
    TIntPr deg = network->getDegree(discoveredNode);
    q.push(TIntPr(deg.Val1, discoveredNode));
    q.push(TIntPr(deg.Val2, discoveredNode));
    visit[discoveredNode] = deg;
  }
}
